package demo.day11;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/7/30-07-30-9:59
 * @Description：demo.day11      数组实现ArrayList
 */
class list{
    private int max = 10;
    private int size = 0;
    private int[] array = new int[max];

    public int getSize(){
        return size;
    }
    public void add(int num){     //添加元素
        if(isFull()){
            growArray();
        }
        array[size] = num;
        size++;
    }
    public void add(int index,int num){
        size++;
        if(isFull()){
            growArray();
        }
        for (int i = size; i > index; i--) {
            array[i] = array[i-1];
        }
        array[index] = num;
    }
    public void addAll(int[] newArr){
        for (int i = 0; i < newArr.length; i++,size++) {
            if(isFull()){
                growArray();
            }
            array[size] = newArr[i];
        }
    }
    public int remove(){
        if(isEmpty()){
            throw new RuntimeException("当前列表为空，删除失败");
        }
        return array[size--];
    }

    public boolean isEmpty() {
        return size == 0;
    }


    public int remove(int index){
        if(index<0 || index > size){
            throw new RuntimeException("要删除的元素不存在");
        }
        int num = array[index];
        for (int i = index; i < size; i++) {
            array[i] = array[i+1];
        }
        size--;
        return num;
    }
    private void growArray() {
        array = Arrays.copyOf(array,max*2);
        max = array.length;
    }

    private boolean isFull() {
        return size == max;
    }
    public void show(){
        for (int i = 0; i < size; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
}
public class ArrayList_use {


    public static void main(String[] args) {
//        list arr = new list();
//        arr.add(1);
//        arr.add(2);
//        arr.add(3);
//        arr.add(2,4);
//        arr.show();
//        arr.remove();
//        arr.remove(1);
//        arr.show();
        ArrayList<String> list = new ArrayList<>();
        list.add("123");
        list.add("12");
        list.add("1234");
        list.add("abc");
        list.add("abc");
        list.add("ab");
        System.out.println(list);
        remove_repe(list);
        System.out.println("去重后的集合：" + list);
    }

    public static void remove_repe(ArrayList<String> list) {
        for (int i = 0; i < list.size()-1; i++) {
            for (int j = i+1; j < list.size(); j++) {
                if(list.get(i).equals(list.get(j))){
                    list.remove(j);
                }
            }
        }

    }

}
